import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import Users from '../components/user/users.vue'
import Rights from '../components/user/rights.vue'
import Roles from '../components/user/roles.vue'
import Cate from '../components/goods/cate.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login,
        
    },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [{
            path:'/welcome',
            component: Welcome
        },{
            path: '/users',
            component: Users
        },{
            path: '/rights',
            component: Rights
        },{
            path:'/roles',
            component: Roles
        },{
            path: '/categories',
            component: Cate
        }]
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach(
    (to, from, next) => {
        // 如果访问登录页面，予以放行
        if (to.path === "/login") return next();
        // 访问内部页面，检查token状态
        let isSession = window.sessionStorage.getItem("token")
        // 如果没有token 强制导航到登陆页面
        if(!isSession) return next("/login")
        // 其他情况予以放行
        return next();
    }
)
export default router